package com.ssau.tnp.clients;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

import math.Matrix;

public class StupidClient implements Runnable {
    private Matrix[] matrix = new Matrix[2];
    private Socket socket = null;

    public StupidClient(String fileNmae1, String fileNmae2) {
	try {
	    matrix[0] = new Matrix();
	    matrix[0].readFromFile(fileNmae1);
	    matrix[1] = new Matrix();
	    matrix[1].readFromFile(fileNmae2);
	} catch (IOException e) {
	    e.printStackTrace();
	}

    }

    @Override
    public void run() {
	int port = 4444;
	String address = "localhost";
	try {
	    System.out.println("Welcome to Client  side");
	    socket = new Socket(address, port);

	    System.out.println("Data transfring to Server...");

	    System.out.println("Matrix 1");
	    for (int i = 0; i < matrix[0].getRows(); i++) {
		for (int j = 0; j < matrix[0].getColumns(); j++) {
		    System.out.print("  " + matrix[0].getElement(i, j) + ",");
		}
		System.out.println();
	    }

	    System.out.println("Matrix 2");
	    for (int i = 0; i < matrix[1].getRows(); i++) {
		for (int j = 0; j < matrix[1].getColumns(); j++) {
		    System.out.print("  " + matrix[1].getElement(i, j) + ",");
		}
		System.out.println();
	    }

	    ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
	    out.writeObject(matrix[0]);
	    out.flush();
	    out.writeObject(matrix[1]);
	    out.flush();

	    System.out.println("Data transfering Done!");

	    ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
	    Matrix resultMatrix = new Matrix((Matrix) in.readObject());
	    System.out.print("RESULT MATRIX\n");
	    for (int i = 0; i < resultMatrix.getRows(); i++) {
		for (int j = 0; j < resultMatrix.getColumns(); j++) {
		    System.out.print(resultMatrix.getElement(i, j) + "  ");
		}
		System.out.println();
	    }
	    in.close();
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }
}
